<!--
 * @Author: 马潭龙
 * @Date: 2022-01-18 15:19:01
 * @LastEditTime: 2022-01-19 19:01:01
 * @LastEditors: 马潭龙
 * @Description: 
 * @FilePath: \devlomp\icm-vue-master\src\views\hifar\regime\regimeLeft.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->
<template>
  <a-card :loading="cardLoading" :bordered="false" style="height: 100%">
    <a-spin :spinning="loading">
      <div class="treeHeader">
        <a-input-search
          v-model="treeSearchParams.unitName"
          allowClear
          size="small"
          placeholder="请输入项目名称"
          enter-button="搜索"
          @keyup.enter.native="loadData"
          @search="loadData"
        />
      </div>
      <a-tree
        :expanded-keys="expandedKeys"
        :auto-expand-parent="autoExpandParent"
        :tree-data="gData"
        :expandedKeys.sync="expandedKeys"
        :selectedKeys="selectedKeys"
        :replaceFields="replaceFields"
        @expand="onExpand"
        @select="handleTreeSelect"
        style="margin-top: 15px"
        class="h-edit-tree"
      >
        <div slot="title" slot-scope="record" class="api-title">
          <a-icon type="folder-add" style="font-size: 14px"/>
          {{ record.createTime }}-{{ record.shortName }}
        </div>
      </a-tree>
    </a-spin>
  </a-card>
</template>

<script>
const searchByKeywords = (params) => getAction('/equip/plEquipmentCategory/treelist', params)
import {getAction, deleteAction} from '@api/manage'
// import regimeLeftModal from './modules/regimeLeftModal'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'

export default {
  name: 'HfPmProjectToolsLeft',
  props: ['value'],
  mixins: [JeecgListMixin],

  data() {
    return {
      cardLoading: true,
      loading: false,
      selectedKeys: [],
      expandedKeys: [],
      autoExpandParent: true,
      nowExpandedKeys: [],
      dataList: [],
      searchValue: '',
      defaultSelectedKeys: [],
      treeSearchParams: {},
      gData: [],
      replaceFields: {
        children: 'children',
        title: 'categoryName',
        key: 'id',
      },
      url: {
        list: '/pm/hfPmProjectRegimeRecords/projectList',
        delete: '/pm/hfPmProjectToolRecords/delete',
      },
    }
  },
  components: {
    // regimeLeftModal,
  },
  created() {
    this.loadData()
    this.commonRequestThen()
  },
  methods: {
    refresh() {
      this.loadData()
    },
    loadData() {
      let params = {
        ...this.treeSearchParams,
      }
      getAction(this.url.list, params).then((res) => {
        let arr = [...res.result]
        this.reWriterWithSlot(arr)
        this.gData = arr
        this.gData = res.result
        console.log('this.gData>>>>', this.gData);
        this.$emit('tree', this.gData)
        if (this.selectedKeys.length <= 0) {
          this.selectedKeys.push(this.gData[0].id)
          this.$emit('getDataByKey', this.selectedKeys[0])
        }
      })
    },
    // 搜索方法
    reWriterWithSlot(arr) {
      for (let item of arr) {
        if (item.children && item.children.length > 0) {
          this.reWriterWithSlot(item.children)
          item.scopedSlots = {title: 'name'}
          let temp = Object.assign({}, item)
          temp.children = {}
          this.dataList.push(temp)
        } else {
          this.dataList.push(item)
          item.scopedSlots = {title: 'name'}
        }
      }
    },
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys
      // 树是否自动展开
      this.autoExpandParent = false
    },
    combineExpandedKeys(gData, value) {
      for (let i = 0; i < gData.length; i++) {
        if (gData[i].categoryName.indexOf(value) > -1) {
          this.nowExpandedKeys.push(gData[i].id)
        }
        if (gData[i].children && gData[i].children.length > 0) {
          this.combineExpandedKeys(gData[i].children, value)
        }
      }
    },
    // 处理联动
    handleTreeSelect(selectedKeys, event) {
      if (selectedKeys.length > 0 && this.selectedKeys[0] !== selectedKeys[0]) {
        this.selectedKeys = [selectedKeys[0]]
        this.$emit('getDataByKey', this.selectedKeys[0])
      }
    },
    commonRequestThen(promise) {
      this.loading = true
      this.loading = false
      this.cardLoading = false
    },
    //左树选中id置空
    claerSelect() {
      this.selectedKeys = []
    },
    // 刷新树
    modalFormOk() {
      this.refresh()
    },
    // 删除树节点
    handleDeleteTree(record) {
      let params = {
        id: record.id,
      }
      this.$confirm({
        title: '确认删除？',
        content: '删除前请确认当前项下无应用数据',
        onOk: () => {
          deleteAction(this.url.delete, params).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.refresh()
            }
          })
        },
      })
    },
  },
}
</script>

<style lang="less" scoped>
.api-title {
  margin-right: 0;

  .actions-btns {
    visibility: hidden;
  }

  &:hover {
    .actions-btns {
      visibility: visible;
    }
  }
}

.treeHeader {
  display: flex;
  justify-content: space-between;
}
</style>